<?php
//区域信息
class App_Region
{
    protected static $_names = array();

	//获取区域名称
	//$id int 区域id
    public static function name ($id)
    {
        if (empty(self::$_names))
            self::build_names();

        return Arr::get(self::$_names, $id, '');
    }

	//获取该地区下的子区域
	//$parentId int 区域id
    public static function optionsByParentId($parentId = 0)
    {
        $query = DB::select()
            ->from('app_const_region')
            ->where('parentId', '=', (int) $parentId)
            ->order_by('id', 'asc')
            ->execute();

        $ret = array(array('id' => 0, 'name' => '全部'));
        foreach ($query as $rs)
        {
            $ret[] = array(
                'id' => $rs['id'],
                'name' => $rs['name'],
            );
        }
        return $ret;
    }

	//构造区域名
    protected static function build_names ()
    {
        $query = DB::select()
            ->from('app_const_region')
            ->execute();

        $ret = array();
        foreach ($query as $rs)
        {
            $id = $rs['id'];
            $name = $rs['name'];
            $ret[$id] = $name;
        }

        self::$_names = $ret;
    }
}
